#!/bin/bash
# script usage:
#   sh gitpull.sh [-d work_dir] [-o master]

while getopts "d:o:" opt; do
  case $opt in
    d)
      # work dir
      work_dir=$OPTARG
      if [ ! -d $work_dir ]; then
        echo "'$work_dir' not an existed directory"
        exit 1
      fi
      ;;
    o)
      # pull origin
      origin=$OPTARG
  esac
done

if [ -n "$work_dir" ]; then
  # to work dir
  cmd="cd $work_dir; "
fi

# git pull
cmd=$cmd"git pull"

if [ -n "$origin" ]; then
  # pull origin
  cmd=$cmd" origin $origin"
fi

# echo $cmd
eval $cmd

exit 0
